package com.palnar.ilecker.ui.common;

import com.palnar.ilecker.R;
import com.palnar.ilecker.baseactivity.BaseActivity;
import com.palnar.ilecker.libs.validator.AbstractValidator;
import com.palnar.ilecker.libs.validator.Form;
import com.palnar.ilecker.libs.validator.Validate;
import com.palnar.ilecker.libs.validator.validator.IpValidator;
import com.palnar.ilecker.libs.validator.validator.NotEmptyValidator;
import com.palnar.ilecker.manager.AppPreferenceManager;
import com.palnar.ilecker.manager.NotifyManager;
import com.palnar.ilecker.manager.SelectorManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ConfigSetting_Activity extends BaseActivity implements OnClickListener{
	
	
	private Context context;
	
	
	private Button btnSave;
	
	private EditText edtIP;
	private EditText edtPort;
	
	private String ip="",port="";
	
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_config);
		context = this;
		
		
		intializeUI();
		setData();
		
		setListners();
		setSelectionManager();
		
	}

	

	private void intializeUI() {
		btnSave=(Button)findViewById(R.id.btnSave);
		edtIP=(EditText)findViewById(R.id.edtIp);
		edtPort=(EditText)findViewById(R.id.edtPort);
				
	}
	private void setData() {
		
		ip=AppPreferenceManager.getIp(context);
		port=AppPreferenceManager.getPort(context);
		
		if((ip!="")&&(port!="")){
			
			edtIP.setText(ip);
			edtPort.setText(port);
		}
		
		
	}

	private void setListners() {
		
		btnSave.setOnClickListener(this);
	}

	private void setSelectionManager() {
		SelectorManager.setBackground(btnSave, SelectorManager
				.getButtonDrawableByScreenCathegory(context,
						R.drawable.global_top_button_normal,
						R.drawable.global_top_button_pressed));
		
	}

	@Override
	public void onClick(View v) {
		if(v==btnSave){
			
			Validate validateIP=new Validate(edtIP);
			
			validateIP.addValidator(new NotEmptyValidator(context));
			validateIP.addValidator(new IpValidator(context));
			
			Validate validatePort=new Validate(edtPort);
			
			validatePort.addValidator(new NotEmptyValidator(context));

			
			Form form=new Form();
			
			form.addValidates(validateIP);
			form.addValidates(validatePort);
			
			if(form.validate()){
			    
			    AppPreferenceManager.saveIp(edtIP.getText().toString(), context);
			    AppPreferenceManager.savePort(edtPort.getText().toString(), context);
			    finish();
			}else{
				
			}

			
		}
		
	}

}
